package Terminal.UI;

import java.awt.Dimension;

import javax.swing.JPanel;

public class PannelliAffiancatiVertical extends PanelComponent{
	
	//private GridLayout gridlay;

	
	public PannelliAffiancatiVertical(PanelComponent parent) {
		super(parent);	
	pannellocomponente = new JPanel(null);
	//pannellocomponente.validate();
	}
	
	public JPanel createComponents() {
		
		return pannellocomponente ;
	}
	
	@Override
	public void add(PanelComponent figlio){
		super.add(figlio);
		JPanel paneltemp= figlio.createComponents();
		//Dimension size = figlio.getSize();
		paneltemp.setBounds(0,0,figlio.getSize().width,figlio.getSize().height);
	
		pannellocomponente.add(paneltemp);
		/*if(size_width<size.width){size_width=size.width;}
		setSize(size_width, size_height+size.height);*/
		updateSize();
		//pannellocomponente.repaint();
	}
	
	public void addPosition(PanelComponent figlio, int y){
		super.add(figlio);
		JPanel paneltemp= figlio.createComponents();
		
		paneltemp.setBounds(5,y,figlio.getSize().width,figlio.getSize().height);

		pannellocomponente.add(paneltemp);
		//pannellocomponente.validate();
		//System.out.print("\npv"+paneltemp.getSize()+" "+paneltemp.getLocation()+" "+children.size());
		updateSize();
		
	}
	
	public void addindex(PanelComponent figlio,int index, int y ){
		super.add(figlio,index);
		JPanel paneltemp= figlio.createComponents();
		//Dimension size = figlio.getSize();
		paneltemp.setBounds(5,y,figlio.getSize().width,figlio.getSize().height);
	
		pannellocomponente.add(paneltemp);
		
		pannellocomponente.validate();
		updateSize();
	}

	@Override
	public void remove(int index) {
		super.remove(index);
		pannellocomponente.remove(index);
		updateSize();
	}
	
	public void removeall(){
		super.removeall();
		pannellocomponente.removeAll();
		updateSize();
		System.out.print("\nsize"+getSize().toString());
	}
	
	
	public void updateSize(){
	
			size_width=0;
			size_height=0;
			int i=0;
			Dimension d= new Dimension(size_width,size_height);
			Dimension d2= new Dimension(size_width,size_height);
		
			
			for (PanelComponent pcelement: children) {
				d=pcelement.getSize();
				if(d.width+pannellocomponente.getComponent(i).getLocation().x>size_width){size_width=d.width+pannellocomponente.getComponent(i).getLocation().x;}
				if(d.height+pannellocomponente.getComponent(i).getLocation().y>size_height){size_height=d.height+pannellocomponente.getComponent(i).getLocation().y;}
				i++;
			}
			
			setSize(size_width+ 10, size_height+10);
			//System.out.print("\n panelvert"+pannellocomponente.getPreferredSize()+" "+pannellocomponente.getSize());
			
			

			if(parent!=null)parent.updateSize();
			
		
	}
	

}
